<?php
require_once 'config.php';
require_once 'conndb.php';
require_once 'domain/template.domain.php';

$templateObj = new Template();

if($_POST['hEdit'] == 'Y') {
    $ID = $_POST['id'];
    $templateName = stripslashes(trim($_POST['template_name']));
    $templateValue = stripslashes(trim($_POST['template_value']));
    $templateName = htmlentities($templateName);
    $templateValue = htmlentities($templateValue);

    $templateName = base64_encode($templateName);
    $templateValue = base64_encode($templateValue);

    $t = new Template();
    $t->setID($ID);
    $t->setTemplateName($templateName);
    $t->setTemplateValue($templateValue);
    $templateObj->updateData($t);

    $msg = "***Update Success***";
}

if($_REQUEST['action'] == 'del') {
    $ID = $_REQUEST['id'];
    $templateObj->deleteData($ID);
    $msg = "***Delete Success***";
}

if($_POST['hAdd'] == 'Y') {
    $templateName = stripslashes(trim($_POST['template_name']));
    $templateValue = stripslashes(trim($_POST['template_value']));
    $templateName = htmlentities($templateName);
    $templateValue = htmlentities($templateValue);

    /*
    $spin = new PhpTextSpinner();
    $contentValue = $spin->spinContent($contentValue);
     *
     */

    $templateName = base64_encode($templateName);
    $templateValue = base64_encode($templateValue);

    $t = new Template();
    $t->setTemplateName($templateName);
    $t->setTemplateValue($templateValue);
    $templateObj->insertData($t);

    $msg = "***Insert Success***";
}

if($_REQUEST['action'] == 'edit') {
    $ID = $_REQUEST['id'];
    $templateEdit = $templateObj->getByID($ID);
}
$templateList = $templateObj->getByObj(new Template());
?>
<?php require_once 'template/header.php'; ?>
<form id="form" action="#" method="post">
    <div style="margin: 0 auto;width: 80%;color: red;text-align: center"><?=$errMsg?></div>
    <div style="margin: 0 auto;width: 80%;color: green;text-align: center"><?=$msg?></div>
    <fieldset style="background: #F2F2F2;"><legend><h3>Template Manage</h3></legend>

        <div id="add" style="width: 90%;margin: 0 auto;background: #FFF;padding: 10px">
            <fieldset>
                <legend>
                    <h3>
                        <?php if($_REQUEST['action'] != 'edit') { ?>
                        Add Template
                            <?php } elseif($_REQUEST['action'] == 'edit') { ?>
                        Edit Content
                        <a href="#" onclick="window.open('template.php','_parent');"><<< Back To Add</a>
                            <?php } ?>
                    </h3>
                </legend>
                <table width="100%">
                    <tr>
                        <td colspan="2">
                            <fieldset style="background: #FFF;">
                                <legend>Tags</legend>
                                <table>
                                    <tr>
                                        <td>
                                            {Title} , {Content} , {AppContent} , {ASIN} ,
                                            {DetailPageURL} , {ListPrice} , {Price} ,
                                            {SmallImage} , {MediumImage} , {CustomerReviews}
                                        </td>
                                    </tr>
                                </table>
                            </fieldset>
                        </td>
                    </tr>
                    <tr>
                        <td>Name :</td>
                        <td>
                            <input type="text" name="template_name" size="100" value="<?php echo $templateEdit[0]['template_name']; ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td>Content :</td>
                        <td>
                            <textarea name="template_value" rows="20" cols="100"><?php echo $templateEdit[0]['template_value']; ?></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>
                            <?php if($_REQUEST['action'] != 'edit') { ?>
                            <input type="submit" value="Add Template" />
                            <input type="hidden" name="hAdd" value="Y" />
                                <?php } elseif($_REQUEST['action'] == 'edit') { ?>
                            <input type="hidden" name="id" value="<?php echo $templateEdit[0]['ID']; ?>" />
                            <input type="hidden" name="hEdit" value="Y" />
                            <input type="hidden" name="action" value="edit" />
                            <input type="submit" value="Edit Template" />
                                <?php } ?>
                        </td>
                    </tr>
                </table>
            </fieldset>
        </div>

        <div id="demo"  style="width: 90%;margin: 0 auto;background: #FFF;padding: 10px;margin-top: 10px;">
            <table cellpadding="0" cellspacing="0" border="0" class="display" id="example" width="100%">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Template Name</th>
                        <th>Delete</th>
                    </tr>
                </thead>
                <tbody>
                    <?php for ($i = 0; $i < count($templateList); $i++) { ?>
                    <tr onclick="edit('<?php echo $templateList[$i]['ID']; ?>')">
                        <td><?php echo $templateList[$i]['ID']; ?></td>
                        <td><?php echo base64_decode($templateList[$i]['template_name']); ?></td>
                        <td align="center"><a href="?id=<?php echo $templateList[$i]['ID']; ?>&action=del">Delete</a></td>
                    </tr>
                        <? } ?>
                </tbody>
                <tfoot>
                    <tr>
                        <th>ID</th>
                        <th>Template Name</th>
                    </tr>
                </tfoot>
            </table>
        </div>
    </fieldset>
</form>
<?php require_once 'template/footer.php'; ?>
<?php require_once 'closedb.php' ?>;
<script type="text/javascript">
    function edit(id) {
        window.location = "?id=" + id + "&action=edit";
    }

    $(document).ready(function() {

        $("#form").validate({
            rules: {
                template_name: "required",
                template_value: "required"
            }
        });
    });
</script>
